package com.cskaoyan.service;

import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.CategoryExample;
import com.cskaoyan.bean.wxcatalog.CatalogData;
import com.cskaoyan.bean.wxcatalog.CurrentCatalogData;
import com.cskaoyan.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: yudefu
 * @date 2021年06月07日  22:36
 * @description: 类目模块
 */

@Service
public class WxWxCatalogServiceImpl implements WxCatalogService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public CurrentCatalogData getCurrentCatalog(Integer id) {

        CurrentCatalogData currentCatalogData = new CurrentCatalogData();

        Category category = categoryMapper.selectByPrimaryKey(id);
        currentCatalogData.setCurrentCategory(category);

        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(category.getId());
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        currentCatalogData.setCurrentSubCategory(categories);

        return currentCatalogData;
    }


    @Override
    public CatalogData getCatalogIndex() {

        CatalogData catalogData = new CatalogData();

        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        categoryExample.setOrderByClause("id" + " " + "asc");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        Category category = categories.get(0);
//        System.out.println(category);
        catalogData.setCurrentCategory(category);

        CategoryExample categoryExampleList = new CategoryExample();
        CategoryExample.Criteria criteriaList = categoryExampleList.createCriteria();
        criteriaList.andPidEqualTo(0);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExampleList);
        catalogData.setCategoryList(categoryList);

        CategoryExample categoryExampleSubList = new CategoryExample();
        CategoryExample.Criteria criteriaSubList = categoryExampleSubList.createCriteria();
        criteriaSubList.andPidEqualTo(category.getId());
        List<Category> categoriesSubList = categoryMapper.selectByExample(categoryExampleSubList);
        catalogData.setCurrentSubCategory(categoriesSubList);


        return catalogData;
    }
}
